<?php

/**
*
* @package phpBB3
* @copyright (c) 2009 kmklr72
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
	exit;
}

class social_network
{
	// Get the mod config array
	function get_sn_config()
	{
		global $db;

		$sn_config = array();
	
		$sql = 'SELECT * FROM ' . SN_CONFIG_TABLE;
		$result = $db->sql_query($sql);

		while ($row = $db->sql_fetchrow($result))
		{
			$sn_config[$row['sn_config_name']] = $row['sn_config_value'];
		}
		$db->sql_freeresult($result);

		return $sn_config;
	}

	// Update the mod config
	function set_sn_config($config_name, $config_value)
	{
		global $db, $sn_config;

		$sql = 'UPDATE ' . SN_CONFIG_TABLE . "
			SET sn_config_value = '" . $db->sql_escape($config_value) . "'
			WHERE sn_config_name = '" . $db->sql_escape($config_name) . "'";
		$db->sql_query($sql);
	}

	// Display vars for ACP pages
	function acp_display_vars($display_vars, $form_key)
	{
		global $db, $user, $auth, $template;
		global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;

		$action	= request_var('action', '');
		$submit = (isset($_POST['submit'])) ? true : false;

		$cfg_array = (isset($_REQUEST['config'])) ? utf8_normalize_nfc(request_var('config', array('' => ''), true)) : $this->new_config;
		$error = array();

		// We validate the complete config if whished
		validate_config_vars($display_vars['vars'], $cfg_array, $error);

		if ($submit && !check_form_key($form_key))
		{
			$error[] = $user->lang['FORM_INVALID'];
		}
		// Do not write values if there is an error
		if (sizeof($error))
		{
			$submit = false;
		}

		// We go through the display_vars to make sure no one is trying to set variables he/she is not allowed to...
		foreach ($display_vars['vars'] as $config_name => $null)
		{
			if (!isset($cfg_array[$config_name]) || strpos($config_name, 'legend') !== false)
			{
				continue;
			}

			$this->new_config[$config_name] = $config_value = $cfg_array[$config_name];

			if ($submit)
			{
				$this->set_sn_config($config_name, $config_value);
			}
		}

		$template->assign_vars(array(
			'L_TITLE'			=> $user->lang[$display_vars['title']],
			'L_TITLE_EXPLAIN'	=> $user->lang[$display_vars['title'] . '_EXPLAIN'],

			'S_ERROR'			=> (sizeof($error)) ? true : false,
			'ERROR_MSG'			=> implode('<br />', $error),

			'U_ACTION'			=> $this->u_action)
		);

		// Output relevant page
		foreach ($display_vars['vars'] as $config_key => $vars)
		{
			if (!is_array($vars) && strpos($config_key, 'legend') === false)
			{
				continue;
			}

			if (strpos($config_key, 'legend') !== false)
			{
				$template->assign_block_vars('options', array(
					'S_LEGEND'		=> true,
					'LEGEND'		=> (isset($user->lang[$vars])) ? $user->lang[$vars] : $vars)
				);

				continue;
			}

			$type = explode(':', $vars['type']);

			$l_explain = '';
			if ($vars['explain'] && isset($vars['lang_explain']))
			{
				$l_explain = (isset($user->lang[$vars['lang_explain']])) ? $user->lang[$vars['lang_explain']] : $vars['lang_explain'];
			}
			else if ($vars['explain'])
			{
				$l_explain = (isset($user->lang[$vars['lang'] . '_EXPLAIN'])) ? $user->lang[$vars['lang'] . '_EXPLAIN'] : '';
			}

			$content = build_cfg_template($type, $config_key, $this->new_config, $config_key, $vars);

			if (empty($content))
			{
				continue;
			}

			$template->assign_block_vars('options', array(
				'KEY'			=> $config_key,
				'TITLE'			=> (isset($user->lang[$vars['lang']])) ? $user->lang[$vars['lang']] : $vars['lang'],
				'S_EXPLAIN'		=> $vars['explain'],
				'TITLE_EXPLAIN'	=> $l_explain,
				'CONTENT'		=> $content,
				)
			);

			unset($display_vars['vars'][$config_key]);
		}
	}

	// Write a comment to the db for a user
	function write_comment($message, $user_id)
	{
		global $db, $user;

		include($phpbb_root_path . 'includes/functions_messenger.' .$phpEx);

		// Check to see if the message is empty
		if (empty($message))
		{
			trigger_error($user->lang['NO_COMMENT']);
		}

		// Do the smilie and bbcode stuff
		$uid = $bitfield = $options = ''; 
		$allow_bbcode = $allow_urls = $allow_smilies = true;

		generate_text_for_storage($comment_text, $uid, $bitfield, $options, $allow_bbcode, $allow_urls, $allow_smilies);

		// Generate the array and insert it in the db
		$sql_ary = array(
			'user_id'			=> $user_id,
			'poster_id'			=> $user->data['user_id'],
			'message'			=> $message,
			'post_time'			=> time(),
			'bbcode_uid'		=> $uid,
    		'bbcode_bitfield'	=> $bitfield,
   			'bbcode_options'	=> $options,
		);

		$sql = 'INSERT INTO ' . SN_COMMENTS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
		$db->sql_query($sql);
	}

	// Send a PM to a user
	function send_pm($status, $sender, $reciever_id)
	{
		global $auth, $db, $user, $phpbb_root_path, $phpEx;

		include($phpbb_root_path . 'includes/functions_privmsgs.' . $phpEx);
		include($phpbb_root_path . 'includes/message_parser.' . $phpEx);

		$message_parser = new parse_message();

		$sql = 'SELECT user_id, username, user_email, user_lang, user_jabber, user_notify_type
			FROM ' . USERS_TABLE . '
			WHERE user_id = ' . $user_id;
		$result = $db->sql_query($sql);

		// Loop through our results
		while ($row = $db->sql_fetchrow($result))
		{
			$contact_users[] = $row;
		}
		$db->sql_freeresult($result);

		$sql = 'SELECT * FROM ' . USERS_TABLE . '
			WHERE user_id = ' . $reciever_id;
		$result = $db->sql_query($sql);

		while ($row = $db->sql_fetchrow($result))
		{
			$sender = $row;
		}
		$db->sql_freeresult($result);

		switch ($status)
		{
			case 'approve':
				$subject = $user->lang['FRAM_APPROVED_SUBJECT'];
				$message_parser->message = sprintf($user->lang['FRAM_APPROVED_MESSAGE'], $sender['username']);
			break;
		}

		$subject = utf8_normalize_nfc($subject);
		$message_parser->message = utf8_normalize_nfc($message_parser->message);
		$message_parser->parse(true, true, true);

		// Set the PM data
		$pm_data = array(
			'from_user_id'		=> $sender['user_id'],
			'from_user_ip'		=> $sender['user_ip'],
			'from_username'		=> $sender['username'],
			'enable_sig'		=> false,
			'enable_bbcode'		=> true,
			'enable_smilies'	=> true,
			'enable_urls'		=> false,
			'icon_id'			=> 0,
			'bbcode_bitfield'	=> $message_parser->bbcode_bitfield,
			'bbcode_uid'		=> $message_parser->bbcode_uid,
			'message'			=> $message_parser->message,
		);

		// Loop through our list of users
		for ($i = 0, $size = sizeof($contact_users); $i < $size; $i++)
		{
			$pm_data['address_list'] = array('u' => array($contact_users[$i]['user_id'] => 'to'));

			submit_pm('post', $subject, $pm_data, true);
		}
	}

	// Get profile data for profile page
	function get_profile_data($user_id)
	{
		global $db;

		$sql = 'SELECT * FROM ' . SN_USER_PROFILE_TABLE . '
			WHERE user_id = ' . $user_id;
		$result = $db->sql_query($sql);
		$row = $db->sql_fetchrow($result);
		$db->sql_freeresult($result);

		return $row;
	}

	// Set profile data
	function set_profile_data($user_id, $data)
	{
		global $db;

		if (!empty($user_data))
		{
			$sql = 'UPDATE ' . SN_USER_PROFILE_TABLE . '
				SET ' . $db->sql_build_array('UPDATE', $data) . '
				WHERE user_id = ' . $user_id;
			$db->sql_query($sql);
		}
		else
		{
			$sql = 'INSERT INTO ' . SN_USERS_PROFILE_TABLE . '
				' . $db->sql_build_array('INSERT', $data) . '
				WHERE user_id = ' . $user_id;
			$db->sql_query($sql);
		}
	}

	function get_unused_blocks($user_id)
	{
		global $db, $template;

		$sql = 'SELECT * FROM ' . SN_USER_BLOCKS_TABLE . '
			WHERE user_id = ' . $user_id;
		$result = $db->sql_query($sql);

		$left_order = $center_order = $right_order = $block_order = '';
		while ($row = $db->sql_fetchrow($result))
		{
			$left_order = $row['left_order'];
			$center_order = $row['center_order'];
			$right_order = $row['right_order'];

			$block_order = $left_order . ',' . $center_order . ',' . $right_order;
		}
		$db->sql_freeresult($result);

		$sql = 'SELECT * FROM ' . SN_BLOCKS_TABLE . '
			WHERE block_id NOT IN(' . $block_order . '
			) ORDER BY block_id ASC';
		$result = $db->sql_query($sql);

		$blockdata = array();
		while ($row = $db->sql_fetchrow($result))
		{
			$blockdata[$row['block_id']] = $row;
		}
		$db->sql_freeresult($result);

		return $blockdata;
	}

	// Generate the block templates
	function block_display($filename)
	{
		global $template;

		$tpl_path = 'sn/blocks/';

		$template->set_filenames(array('file' => $tpl_path . $filename));

		return $template->assign_display('file');
	}

	// Generate the default blocks
	function get_def_blocks()
	{
		global $db, $user, $auth, $template;
		global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
		global $sn;

		$sql = 'SELECT * FROM ' . SN_BLOCKS_TABLE . ' ORDER BY block_id ASC';
		$result = $db->sql_query($sql);

		while ($row = $db->sql_fetchrow($result))
		{
			if (!empty($row['paginate_file']))
			{
				include($phpbb_root_path . 'sn_blocks/pagination/' . $row['paginate_file'] . '.' . $phpEx);
			}

			if (!empty($row['block_php_file']))
			{
				include($phpbb_root_path . 'sn_blocks/' . $row['block_php_file'] . '/' . $row['block_php_file'] . '.' . $phpEx);
			}
			$template->assign_var('BLOCK_NAME', $row['block_name']);

			switch ($row['block_pos'])
			{
				case 'L':
					$template->assign_block_vars('left_blocks', array(
						'S_BLOCK_ID'			=> $row['block_id'],
						'S_BLOCK_FILE'			=> $this->block_display($row['block_html_file']),
					));
				break;

				case 'C':
					$template->assign_block_vars('center_blocks', array(
						'S_BLOCK_ID'			=> $row['block_id'],
						'S_BLOCK_FILE'			=> $this->block_display($row['block_html_file']),
					));
				break;

				case 'R':
					$template->assign_block_vars('right_blocks', array(
						'S_BLOCK_ID'			=> $row['block_id'],
						'S_BLOCK_FILE'			=> $this->block_display($row['block_html_file']),
					));
				break;

				default:
					trigger_error('NO_BLOCKS');
				break;
			}
		}
		$db->sql_freeresult($result);

		return;
	}

	function replace_block_str(&$str)
	{
		$str = str_replace('center_1', '', $str);
		$str = str_replace('string=', '', $str);
		$str = str_replace('&amp;', ',', $str);
		// This one is for the re-created cookies. Just another part of the strange cookie system I have working here.
		$str = str_replace(':', ',', $str);

		return $str;
	}

	// Get a user's blocks
	function get_user_blocks($user_id)
	{
		global $db, $user, $auth, $template;
		global $config, $table_prefix, $phpbb_root_path, $phpbb_admin_path, $phpEx;
		global $sn;

		$sql = 'SELECT * FROM ' . SN_BLOCKS_TABLE . ' ORDER BY block_id ASC';
		$result = $db->sql_query($sql);

		$blockdata = array();
		while ($row = $db->sql_fetchrow($result))
		{
			$blockdata[$row['block_id']] = $row;
		}
		$db->sql_freeresult($result);

		$sql = 'SELECT * FROM ' . SN_USER_BLOCKS_TABLE . ' WHERE user_id = ' . $user_id;
		$result = $db->sql_query($sql);

		$left_order = $center_order = $right_order = '';
		while ($row = $db->sql_fetchrow($result))
		{
			$left_order = $row['left_order'];
			$center_order = $row['center_order'];
			$right_order = $row['right_order'];

			$block_str = $row['left_order'] . ',' . $row['center_order'] . ',' . $row['right_order'];
			$block_ary = explode(',', $block_str);
		}
		$db->sql_freeresult($result);

		// If the user doesn't have a custom setup, load the default
		if ($left_order == '' && $center_order == '' && $right_order == '')
		{
			$this->get_def_blocks();
			return;
		}

		// Generate the blocks
		foreach ($block_ary as $block_id)
		{
			// Set block positions
			$block_num = (string) $block_id;
			if (stripos($left_order, $block_num) !== false)
			{
				$blockdata[$block_id]['block_pos'] = 'L';
			}
			elseif (stripos($center_order, $block_num) !== false)
			{
				$blockdata[$block_id]['block_pos'] = 'C';
			}
			elseif (stripos($right_order, $block_num) !== false)
			{
				$blockdata[$block_id]['block_pos'] = 'R';
			}

			// Include pagination files first for multiple sets of pagination
			if (!empty($blockdata[$block_id]['paginate_file']))
			{
				include($phpbb_root_path . 'sn_blocks/pagination/' . $blockdata[$block_id]['paginate_file'] . '.' . $phpEx);
			}

			// Include block php files
			if (!empty($blockdata[$block_id]['block_php_file']))
			{
				include($phpbb_root_path . 'sn_blocks/' . $blockdata[$block_id]['block_php_file'] . '/' . $blockdata[$block_id]['block_php_file'] . '.' . $phpEx);
			}
			$template->assign_var('BLOCK_NAME', $blockdata[$block_id]['block_name']);

			// Finally display the blocks
			switch ($blockdata[$block_id]['block_pos'])
			{
				case 'L':
					$template->assign_block_vars('left_blocks', array(
						'S_BLOCK_ID'			=> $blockdata[$block_id]['block_id'],
						'S_BLOCK_FILE'			=> $this->block_display($blockdata[$block_id]['block_html_file']),
					));
				break;

				case 'C':
					$template->assign_block_vars('center_blocks', array(
						'S_BLOCK_ID'			=> $blockdata[$block_id]['block_id'],
						'S_BLOCK_FILE'			=> $this->block_display($blockdata[$block_id]['block_html_file']),
					));
				break;

				case 'R':
					$template->assign_block_vars('right_blocks', array(
						'S_BLOCK_ID'			=> $blockdata[$block_id]['block_id'],
						'S_BLOCK_FILE'			=> $this->block_display($blockdata[$block_id]['block_html_file']),
					));
				break;
			}
		}

		return;
	}

	// Get block info
	function get_block_info($block_id)
	{
		global $db;

		$sql = 'SELECT * FROM ' . SN_BLOCKS_TABLE . '
			WHERE block_id = ' . $block_id;
		$result = $db->sql_query($sql);
		$row = $db->sql_fetchrow($result);
		$db->sql_freeresult($result);

		return $row;
	}

	// Get user mood
	function get_user_mood($user_id)
	{
		global $db, $template;

		$sql = 'SELECT * FROM ' . USERS_TABLE . '
			WHERE user_id = ' . $user_id;
		$result = $db->sql_query($sql);
		$user_data = $db->sql_fetchrow($result);
		$db->sql_freeresult($result);

		$sql = 'SELECT * FROM ' . SN_MOODS_TABLE . "
			WHERE mood_name = '" . $user_data['user_mood'] . "'";
		$result = $db->sql_query($sql);

		while ($row = $db->sql_fetchrow($result))
		{
			$template->assign_vars(array(
				'USER_MOOD'			=> $row['mood_name'],
				'MOOD_IMAGE'		=> '<img src="' . $phpbb_root_path . 'images/moods/' . $row['mood_image'] . '" alt="" />' . $row['mood_name'],
			));
		}
		$db->sql_freeresult($result);

		return;
	}

	// Generate dropdown of moods
	function list_moods($user_id)
	{
		global $db;

		$sql = 'SELECT user_mood as current_mood FROM ' . USERS_TABLE . '
			WHERE user_id = ' . $user_id;
		$result = $db->sql_query($sql);
		$current_mood = $db->sql_fetchfield('current_mood');
		$db->sql_freeresult($result);

		$sql = 'SELECT mood_id, mood_name
			FROM ' . SN_MOODS_TABLE . '
			ORDER BY mood_id';
		$result = $db->sql_query($sql);

		$options = '<select name="mood" id="mood"><option value="0">-----------</option>';

		while ($row = $db->sql_fetchrow($result))
		{
			// Create the options
			$selected = ($row['mood_id'] == $current_mood) ? ' selected="selected"' : '';
			$options .= '<option value="' . $row['mood_id'] . '"' . $selected . '>' . $row['mood_name'] . '</option>';
		}
		$db->sql_freeresult($result);

		$options .= '</select>';

		$template->assign_var('MOOD_SELECT', $options);

		return;
	}

	// Set user's mood
	function set_mood($user_id, $user_mood)
	{
		global $db;

		$sql = 'UPDATE ' . USERS_TABLE . "
			SET user_mood = '" . $user_mood . "'
			WHERE user_id = " . $user_id;
		$db->sql_query($sql);
	}

	// Create club
	function create_club($club_ary)
	{
		global $db, $user;

		$sql_ary = array(
			'club_name'			=> $club_ary['club_name'],
			'club_desc'			=> $club_ary['club_desc'],
			'club_email'		=> $club_ary['club_email'],
			'club_reason'		=> $club_ary['club_reason'],
			'num_members'		=> 1,
			'creation_time'		=> time(),
			//'last_comment'		=> '',
			//'last_commentor'	=> '',
		);

		$sql = 'INSERT INTO ' . SN_CLUBS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
		$db->sql_query($sql);

		$sql = 'SELECT * FROM ' . SN_CLUBS_TABLE . "
			WHERE club_name = '" . $club_ary['club_name'] . "'";
		$result = $db->sql_query($sql);
		$row = $db->sql_fetchrow($result);
		$db->sql_freeresult($result);

		$sql = 'UPDATE ' . USERS_TABLE . '
			SET user_club_id = ' . $row['club_id'] . '
			WHERE user_id = ' . $user->data['user_id'];
		$db->sql_query($sql);
	}

	// Get list of clubs
	function get_clubs_list()
	{
		global $db, $template, $phpbb_root_path, $phpEx;

		$sql = 'SELECT * FROM ' . SN_CLUBS_TABLE;
		$result = $db->sql_query($sql);

		while ($row = $db->sql_fetchrow($result))
		{
			$template->assign_block_vars('clubs', array(
				'CLUB_NAME'							=> $row['club_name'],
				'CLUB_DESC'							=> $row['club_desc'],
				'NUM_MEMBERS'						=> $row['num_members'],
				'CLUB_COMMENTS'						=> $row['num_comments'],
				//'LAST_CLUB_COMMENTER_FULL'			=> $row['last_commenter'],
				//'U_LAST_CLUB_COMMENT'				=> append_sid("{$phpbb_root_path}clubs.$phpEx", 'mode=last_comment&amp;club=' . $club_id),
				//'LAST_CLUB_COMMENT_TIME'			=> $row['last_comment_time'],

				'U_VIEW_CLUB'						=> append_sid("{$phpbb_root_path}clubs.$phpEx", 'mode=info&amp;cid=' . $row['club_id']),
			));
		}
		$db->sql_freeresult($result);

		return;
	}

	// Get club info
	function get_club_info($club_id)
	{
		global $db;

		$sql = 'SELECT * FROM ' . SN_CLUBS_TABLE . '
			WHERE club_id = ' . $club_id;
		$result = $db->sql_query($sql);
		$row = $db->sql_fetchrow($result);
		$db->sql_freeresult($result);

		return $row;
	}

	/*
	*  Modified version of generate_pagination() (Fix for Ticket #48835:  http://www.phpbb.com/bugs/phpbb3/ticket.php?ticket_id=48835)
	*  This function allows us to use multiple sets of pagination on one page
	*/
	//function paginate_this($base_url, $num_items, $per_page, $start_item = 'start', $start_params = array(), $add_prevnext_text = false)
	function paginate_this($base_url, $num_items, $per_page, $start_item, $add_prevnext_text = false, $tpl_prefix = '', $start_name = 'start', $start_params = array())
	{
		global $template, $user;

		// Make sure $per_page is a valid value
		$per_page = ($per_page <= 0) ? 1 : $per_page;

		// Maintain params for other pagination sets
		$param_url = '';
		$page_one = '';
		$js_params = '';
		foreach ($start_params as $start_param)
		{
			// Make sure the param is a string
			$start_param = (string) $start_param;

			// Check if the param exists in the URL
			if (isset($_GET[$start_param]))
			{
				$param_url .= '&' . $start_param . '=' . request_var($start_param, 0);

				if ($start_param != $start_name)
				{
					$page_one .= '&' . $start_param . '=' . request_var($start_param, 0);
				}
			}

			// Replace the parameter we are changing here if it exists otherwise we just keep the generated URL
			if ($start_param == $start_name && isset($_GET[$start_name]))
			{
				$param_url = str_replace('&' . $start_name . '=' . request_var($start_name, 0), '', $param_url);
			}
		}

		$seperator = '<span class="page-sep">' . $user->lang['COMMA_SEPARATOR'] . '</span>';
		$total_pages = ceil($num_items / $per_page);

		if ($total_pages == 1 || !$num_items)
		{
			return false;
		}

		$on_page = floor($start_item / $per_page) + 1;
		$url_delim = (strpos($base_url, '?') === false) ? '?' : '&amp;';

		$page_string = ($on_page == 1) ? '<strong>1</strong>' : '<a href="' . $base_url . $page_one . '">1</a>';

		if ($total_pages > 5)
		{
			$start_cnt = min(max(1, $on_page - 4), $total_pages - 5);
			$end_cnt = max(min($total_pages, $on_page + 4), 6);

			$page_string .= ($start_cnt > 1) ? ' ... ' : $seperator;

			for ($i = $start_cnt + 1; $i < $end_cnt; $i++)
			{
				$page_string .= ($i == $on_page) ? '<strong>' . $i . '</strong>' : '<a href="' . $base_url . "{$url_delim}" . $start_name . "=" . (($i - 1) * $per_page) . '' . $param_url . '">' . $i . '</a>';
				if ($i < $end_cnt - 1)
				{
					$page_string .= $seperator;
				}
			}

			$page_string .= ($end_cnt < $total_pages) ? ' ... ' : $seperator;
		}
		else
		{
			$page_string .= $seperator;

			for ($i = 2; $i < $total_pages; $i++)
			{
				$page_string .= ($i == $on_page) ? '<strong>' . $i . '</strong>' : '<a href="' . $base_url . "{$url_delim}" . $start_name . "=" . (($i - 1) * $per_page) . '' . $param_url . '">' . $i . '</a>';
				if ($i < $total_pages)
				{
					$page_string .= $seperator;
				}
			}
		}

		$page_string .= ($on_page == $total_pages) ? '<strong>' . $total_pages . '</strong>' : '<a href="' . $base_url . "{$url_delim}" . $start_name . "=" . (($total_pages - 1) * $per_page) . '' . $param_url . '">' . $total_pages . '</a>';

		if ($add_prevnext_text)
		{
			if ($on_page != 1)
			{
				$page_string = '<a href="' . $base_url . "{$url_delim}" . $start_name . "=" . (($on_page - 2) * $per_page) . '' . $param_url . '">' . $user->lang['PREVIOUS'] . '</a>&nbsp;&nbsp;' . $page_string;
			}

			if ($on_page != $total_pages)
			{
				$page_string .= '&nbsp;&nbsp;<a href="' . $base_url . "{$url_delim}" . $start_name . "=" . ($on_page * $per_page) . '' . $param_url . '">' . $user->lang['NEXT'] . '</a>';
			}
		}

		$template->assign_vars(array(
			$tpl_prefix . 'BASE_URL'		=> $base_url,
			'A_' . $tpl_prefix . 'BASE_URL'	=> addslashes($base_url),
			$tpl_prefix . 'PER_PAGE'		=> $per_page,

			$tpl_prefix . 'PREVIOUS_PAGE'	=> ($on_page == 1) ? '' : $base_url . "{$url_delim}" . $start_name . "=" . (($on_page - 2) * $per_page),
			$tpl_prefix . 'NEXT_PAGE'		=> ($on_page == $total_pages) ? '' : $base_url . "{$url_delim}" . $start_name . "=" . ($on_page * $per_page),
			$tpl_prefix . 'TOTAL_PAGES'		=> $total_pages,
		));

		return $page_string;
	}

	function get_user_data()
	{
		global $db;

		$sql = 'SELECT * FROM ' . USERS_TABLE;
		$result = $db->sql_query($sql);

		while ($row = $db->sql_fetchrow($result))
		{
			$userdata[$row['user_id']] = $row;
		}
		$db->sql_freeresult($result);

		return $userdata;
	}

	function get_zebra_data()
	{
		global $db;

		$zebradata = array();

		$sql = 'SELECT * FROM ' . ZEBRA_TABLE;
		$result = $db->sql_query($sql);

		while ($row = $db->sql_fetchrow($result))
		{
			$zebradata['u'][$row['user_id']][$row['zebra_id']] = $row;
			$zebradata['z'][$row['zebra_id']][$row['user_id']] = $row;
		}
		$db->sql_freeresult($result);

		return $zebradata;
	}
}

?>